<?php
include ("include.php");
$thanhvien = new class_thanhvien();

$title = "&#272;&#259;ng k&yacute; th&agrave;nh vi&ecirc;n";

$langid = $HTTP_GET_VARS["langid"];
$langid = getNumber($langid);

//Layout
$layout_file = $arr_layout["sub"];
include("html_layout.php");

$session_return_page = $HTTP_SESSION_VARS["session_return_page"];

if (isset($HTTP_GET_VARS['action']) && ($HTTP_GET_VARS['action'] == 'process')) {
	$username = $HTTP_POST_VARS["username"];
	$password = $HTTP_POST_VARS["password"];
	$password_2 = $HTTP_POST_VARS["password_2"];
	$email = $HTTP_POST_VARS["email"];
	//echo "username: $username - password: $password<br>";
	//kiem tra user & password
	$arr_users = $thanhvien->check_username($username);
	if(count($arr_users) > 0){//kiem tra username
		$register_fail = 1;
		$taikhoan_msg .= "T&ecirc;n &#273;&#259;ng nh&#7853;p &#273;&atilde; t&#7891;n t&#7841;i<br>";
	}else{//kiem tra password
		//
		if ($password != $password_2 || $password == "" || $username == ""){
			$register_fail = 1;
			$matkhau_msg .= "M&#7853;t kh&#7849;u kh&ocirc;ng gi&#7889;ng nhau, vui l&ograve;ng nh&#7853;p l&#7841;i!<br>";
		}
	}
	if($email == ""){
		$register_fail = 1;
		$email_msg = "Vui l&ograve;ng nh&#7853;p &#273;&#7883;a ch&#7881; email!<br>";
	}
	
	if (!$register_fail){
		//chen vao database
		$table = "thanhvien";
		
		if($password != ""){
			$password = tep_encrypt_password($password);
		}
		
		$fields_arr = array(
			"username" => "'$username'",
			"password" => "'$password'",
			"hoten" => "'$hoten'",
			"diachi" => "'$diachi'",
			"dienthoai" => "'$dienthoai'",
			"trangthai" => "'0'",
			"email" => "'$email'"
		);
		insert($table, $fields_arr);
		
		$arr_user_login = array();
		$arr_user_login['user_id'] = $arr_users['id'];
		$arr_user_login['user_name'] = $arr_users['username'];
		$arr_user_login['user_level'] = $arr_users['level'];
		
		session_unregister('session_user_login');	$session_user_login = $arr_user_login;	session_register('session_user_login');
		/*
		echo "<script>document.location='home.php'</script>";
		//*/
		
		$msg_thanhcong = "
			B&#7841;n &#273;&atilde; &#273;&#259;ng k&yacute; th&agrave;nh c&ocirc;ng!<br />
			T&agrave;i kho&#7843;n c&#7911;a b&#7841;n &#273;ang ch&#7901; ch&uacute;ng t&ocirc;i xem x&eacute;t &#273;&#7875; k&iacute;ch ho&#7841;t.  <br />	
		";
	}
	
}else{
	//
}

if($msg_thanhcong != ""){
	$str_content = $msg_thanhcong;
}else{
	$str_content = $thanhvien->FormRegister();
}


$str_htm = ereg_replace("@noidung@",$str_content, $str_htm);
$str_htm = ereg_replace("<!--taikhoan_err-->",$taikhoan_msg, $str_htm);
$str_htm = ereg_replace("<!--matkhau_err-->",$matkhau_msg, $str_htm);
$str_htm = ereg_replace("<!--email_err-->",$email_msg, $str_htm);



include ("include_2.php");

print $str_htm;
?>